public class TextMessage extends Message {

    private DialogHB dialoghb;

    public TextMessage(TIMMessage message){
        this.message = message;
    }

    public TextMessage(String s){
        message = new TIMMessage();
        TIMTextElem elem = new TIMTextElem();
        elem.setText(s);
        message.addElement(elem);
    }

    public TextMessage(TIMMessageDraft draft){
        message = new TIMMessage();
        for (TIMElem elem : draft.getElems()){
            message.addElement(elem);
        }
    }

    private List<ImageSpan> sortByIndex(final Editable editInput, ImageSpan []array){
        ArrayList<ImageSpan> sortList = new ArrayList<>();
        for (ImageSpan span : array){
            sortList.add(span);
        }
        Collections.sort(sortList, new Comparator<ImageSpan>() {
            @Override
            public int compare(ImageSpan lhs, ImageSpan rhs) {
                return editInput.getSpanStart(lhs) - editInput.getSpanStart(rhs);
            }
        });
        return sortList;
    }

    public TextMessage(Editable s){
        message = new TIMMessage();
        ImageSpan[] spans = s.getSpans(0, s.length(), ImageSpan.class);
        List<ImageSpan> listSpans = sortByIndex(s, spans);
        int currentIndex = 0;
        for (ImageSpan span : listSpans){
            int startIndex = s.getSpanStart(span);
            int endIndex = s.getSpanEnd(span);
            if (currentIndex < startIndex){
                TIMTextElem textElem = new TIMTextElem();
                textElem.setText(s.subSequence(currentIndex, startIndex).toString());
                message.addElement(textElem);

            }
            TIMFaceElem faceElem = new TIMFaceElem();
            int index = Integer.parseInt(s.subSequence(startIndex, endIndex).toString());
            faceElem.setIndex(index);
            if (index < EmoticonUtil.emoticonData.length){
                faceElem.setData(EmoticonUtil.emoticonData[index].getBytes(Charset.forName("UTF-8")));
            }
            message.addElement(faceElem);
            currentIndex = endIndex;
        }
        if (currentIndex < s.length()){
            TIMTextElem textElem = new TIMTextElem();
            textElem.setText(s.subSequence(currentIndex, s.length()).toString());
            message.addElement(textElem);
        }

    }
    
    @Override
    public String getSummary() {
        String xiaoxi="";
        String str = getRevokeSummary();
        if (str != null) return str;
        StringBuilder result = new StringBuilder();

        for (int i = 0; i<message.getElementCount(); ++i){
            switch (message.getElement(i).getType()){
                case Face:
                    TIMFaceElem faceElem = (TIMFaceElem) message.getElement(i);
                    byte[] data = faceElem.getData();
                    if (data != null){
                        result.append(new String(data, Charset.forName("UTF-8")));
                    }
                    break;
                case Text:
                    TIMTextElem textElem = (TIMTextElem) message.getElement(i);
                    result.append(textElem.getText());
                    break;
            }

        }
        xiaoxi=result.toString();
        return xiaoxi;
    }

    @Override
    public void save() {
    }

    private static int getNumLength(int n){
        return String.valueOf(n).length();
    }

    public static SpannableStringBuilder getString(List<TIMElem> elems, Context context){
        SpannableStringBuilder stringBuilder = new SpannableStringBuilder();
        for (int i = 0; i<elems.size(); ++i){
            switch (elems.get(i).getType()){
                case Face:
                    TIMFaceElem faceElem = (TIMFaceElem) elems.get(i);
                    int startIndex = stringBuilder.length();
                    try{
                        AssetManager am = context.getAssets();
                        InputStream is = am.open(String.format("emoticon/%d.png", faceElem.getIndex()));
                        if (is == null) continue;
                        Bitmap bitmap = BitmapFactory.decodeStream(is);
                        Matrix matrix = new Matrix();
                        int width = bitmap.getWidth();
                        int height = bitmap.getHeight();
                        matrix.postScale(PxUtil.emo_bian2, PxUtil.emo_bian2);
                        Bitmap resizedBitmap = Bitmap.createBitmap(bitmap, 0, 0, width, height, matrix, true);
                        CenterAlignImageSpan span2 = new CenterAlignImageSpan(context,resizedBitmap);
                        stringBuilder.append(String.valueOf(faceElem.getIndex()));
                        stringBuilder.setSpan(span2, startIndex, startIndex + getNumLength(faceElem.getIndex()), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
                        is.close();
                    }catch (IOException e){
                    }
                    break;
                case Text:
                    TIMTextElem textElem = (TIMTextElem) elems.get(i);
                    stringBuilder.append(textElem.getText());
                    break;
            }
        }
        return stringBuilder;
    }

    @Override
    public void showMessage(ChatAdapter.ViewHolder viewHolder, final Context context) {
        clearView(viewHolder,true,context);
        if (checkRevoke(viewHolder)) return;
        boolean hasText = false;
        List<TIMElem> elems = new ArrayList<>();
        for (int i = 0; i < message.getElementCount(); ++i){
            elems.add(message.getElement(i));
            if (message.getElement(i).getType() == TIMElemType.Text){
                hasText = true;
            }
        }
        SpannableStringBuilder stringBuilder = getString(elems, context);
        if (!hasText){
            stringBuilder.insert(0," ");
        }
        checkzi(stringBuilder,viewHolder,context);
        showStatus(viewHolder);
    }

    private void checkzi(SpannableStringBuilder stringBuilder, ChatAdapter.ViewHolder viewHolder,final Context context){
        LinearLayout linearLayout = new LinearLayout(MyApplication.getContext());
        if(message.isSelf()){
            linearLayout.setBackgroundResource(R.drawable.chat_r);
        }else {
            linearLayout.setBackgroundResource(R.drawable.chat_l);
        }
        linearLayout.setGravity(Gravity.CENTER);
        int a=PxUtil.text_side;
            TextView tv = new TextView(MyApplication.getContext());
            tv.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 14.5f);
            tv.setMaxWidth((PxUtil.getW()/20)*13);
            tv.setLineSpacing(0f,1.2f);
            tv.setTextColor(0xff353535);
            tv.setText(stringBuilder);
            linearLayout.addView(tv);
        getBubbleView(viewHolder,true,context).addView(linearLayout);
    }

}
